﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.SEO.Business;
using com.SEO.Model;
using ExtAspNet;
using Helper.Bussiness;
using System.Data;

namespace SEO_Server.Pages
{
    public partial class RankQueryPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                dpkStartDate.SelectedDate = DateTime.Now.AddMonths(-1);
                dpkEndDate.SelectedDate = DateTime.Now;

                BindDropDownList();
            }
        }

        #region Private Method

        /// <summary>
        /// Bind Grid
        /// </summary>
        private void BindGrid()
        {
            DateTime startDate = Convert.ToDateTime(dpkStartDate.SelectedDate);
            DateTime endDate = (Convert.ToDateTime(dpkEndDate.SelectedDate)).AddMonths(1).AddDays(-1).AddHours(23).AddMinutes(59).AddSeconds(59);
            string strSiteID = ddlstSiteURL.SelectedValue;
            string strKeyword = tbxkeyword.Text.Trim();

            KeywordManage keywordManage = new KeywordManage();
            RankManage rankManage = new RankManage();
            DataTable dataTable = new DataTable();
            int nTotalYahooDays = 0;
            int nTotalGoogleDays = 0;

            List<KeywordInfo> lstKeywordInfo = keywordManage.GetKeywordInfoByCondition(" SiteID = '" + strSiteID
                + "' and Keyword like '%" + strKeyword + "%'");
            dataTable.Columns.Add(new DataColumn() { ColumnName = "Keyword" });
            dataTable.Columns.Add(new DataColumn() { ColumnName = "Yahoo" });
            dataTable.Columns.Add(new DataColumn() { ColumnName = "Google" });

            foreach (KeywordInfo info in lstKeywordInfo)
            {
                nTotalYahooDays = 0;
                nTotalGoogleDays = 0;

                List<RankInfo> lstRankInfo = rankManage.getRankInfoByCondition(" KeywordID = '" + info.KeywordId.ToString()
                    + "' and (RankDate between '" + startDate.ToString()
                    + "' and  '" + endDate.ToString() + "')");

                foreach (RankInfo rankInfo in lstRankInfo)
                {
                    nTotalYahooDays += rankInfo.Yahoo;
                    nTotalGoogleDays += rankInfo.Google;
                }

                DataRow row = dataTable.NewRow();
                row["Keyword"] = info.Keyword;
                row["Yahoo"] = nTotalYahooDays;
                row["Google"] = nTotalGoogleDays;
                dataTable.Rows.Add(row);
            }

            gridRank.DataSource = dataTable;
            gridRank.DataBind();
        }

        /// <summary>
        /// Bind Drop Down List
        /// </summary>
        private void BindDropDownList()
        {
            SiteManage siteManage = new SiteManage();
            List<SiteInfo> lstSite = siteManage.GetSiteByCondition(" 1 = 1");
            foreach (SiteInfo info in lstSite)
            {
                ddlstSiteURL.Items.Add(new ExtAspNet.ListItem() { Text = info.SiteURL, Value = info.SiteId.ToString() });
            }
        }

        #endregion

        #region Page Event

        /// <summary>
        /// Search Event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (ddlstSiteURL.SelectedIndex == 0)
            {
                Alert.Show("請選擇站點URL!");
                return;
            }

            if (tbxkeyword.Text.Trim().Length == 0)
            {
                Alert.Show("請輸入關鍵字!");
                return;
            }

            DateTime startDate = Convert.ToDateTime(dpkStartDate.SelectedDate);
            DateTime endDate = Convert.ToDateTime(dpkEndDate.SelectedDate);

            if (DateTime.Compare(startDate, endDate) > 0)
            {
                Alert.Show("開始日期不可大於結束日期!");
            }

            BindGrid();
        }

        /// <summary>
        /// Data Bound Event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gridRank_RowDataBound(object sender, ExtAspNet.GridRowEventArgs e)
        {

        }

        /// <summary>
        /// 設置關鍵字
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSetKeyword_Click(object sender, EventArgs e)
        {
            if (ddlstSiteURL.SelectedIndex == 0)
            {
                Alert.Show("請選擇站點URL!");
                return;
            }

            wndSettingKeyword.IFrameUrl = "KeywordSettingPage.aspx?SiteID=" + ddlstSiteURL.SelectedValue;
            wndSettingKeyword.Hidden = false;
        }

        #endregion
    }
}